package com.scholarship.system.controller;

import com.scholarship.system.common.dto.Result;
import com.scholarship.system.entity.Teacher;
import com.scholarship.system.service.TeacherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 导师控制器
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    private static final Logger log = LoggerFactory.getLogger(TeacherController.class);

    @Autowired
    private TeacherService teacherService;

    /**
     * 添加导师信息 - 注册过程中使用，无需认证
     */
    @PostMapping("/add")
    public Result<Void> add(@Valid @RequestBody Teacher teacher) {
        log.info("添加导师信息: teacherId={}", teacher.getTeacherId());
        try {
            teacherService.addTeacher(teacher);
            return Result.success();
        } catch (Exception e) {
            log.error("添加导师信息失败", e);
            return Result.error("添加导师信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取导师列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('ROLE_TEACHER')")
    public Result<List<Teacher>> list() {
        return Result.success(teacherService.getTeacherList());
    }

    /**
     * 根据ID获取导师信息
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('ROLE_TEACHER')")
    public Result<Teacher> getById(@PathVariable Long id) {
        return Result.success(teacherService.getTeacherById(id));
    }

    /**
     * 更新导师信息
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('ROLE_TEACHER')")
    public Result<Void> update(@Valid @RequestBody Teacher teacher) {
        teacherService.updateTeacher(teacher);
        return Result.success();
    }

    /**
     * 删除导师
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('ROLE_TEACHER')")
    public Result<Void> delete(@PathVariable Long id) {
        teacherService.deleteTeacher(id);
        return Result.success();
    }
}